package com.shizhanzhe.zxbapp.my;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.Util;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareCodeActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    RelativeLayout topBack;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_ewm)
    ImageView ivEwm;
    @BindView(R.id.wx)
    Button wx;
    @BindView(R.id.pyq)
    Button pyq;
    @BindView(R.id.qq)
    Button qq;
    @BindView(R.id.xz)
    Button xz;
    @BindView(R.id.tv_false)
    TextView tvFalse;
    @BindView(R.id.ll_share)
    LinearLayout llShare;
    @BindView(R.id.rl)
    RelativeLayout rl;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_code);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .transparentBar()
                .init();
        ivEwm.setImageBitmap(Util.encodeAsBitmap(Path.zxb_register(UserBean.ip,UserBean.uid)));
    }

    @OnClick({R.id.top_back, R.id.iv_more, R.id.tv_false, R.id.wx, R.id.pyq, R.id.qq, R.id.xz})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.iv_more:
                //分享
                Util.saveBitmap(this, getBitmapByView(rl), "/invite.jpg");
                File file = new File(Environment.getExternalStorageDirectory() + "/test/invite.jpg");
                if (file.exists()) {
                    //Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                    llShare.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.tv_false:
                llShare.setVisibility(View.GONE);
                break;
            case R.id.wx:
                Wechat.ShareParams sp = new Wechat.ShareParams();
                sp.setImagePath(Environment.getExternalStorageDirectory() + "/test/invite.jpg");
                sp.setShareType(Platform.SHARE_IMAGE);
                Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
                weChat.share(sp);
                //微信
                break;
            case R.id.pyq:
                //朋友圈
                Platform.ShareParams weM = new Platform.ShareParams();
                weM.setImagePath(Environment.getExternalStorageDirectory() + "/test/invite.jpg");
                weM.setShareType(Platform.SHARE_IMAGE);
                Platform WechatM = ShareSDK.getPlatform(WechatMoments.NAME);
                WechatM.share(weM);
                break;
            case R.id.qq:
                //QQ
                QQ.ShareParams spqq = new Wechat.ShareParams();
                spqq.setImagePath(Environment.getExternalStorageDirectory() + "/test/invite.jpg");
                spqq.setShareType(Platform.SHARE_IMAGE);
                Platform Q = ShareSDK.getPlatform(QQ.NAME);
                Q.share(spqq);
                break;
            case R.id.xz:
                //保存
                Util.saveBitmap(this, getBitmapByView(rl), System.currentTimeMillis() + ".jpg");
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private Bitmap getBitmapByView(View headerView) {
        int h = headerView.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        headerView.draw(canvas);
        return bitmap;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
